package GUI;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import ctrLayer.TreeCtr;

/**
 * CreateTreeFrame
 * @author Thomas
 */
public class CreateTreeFrame extends JFrame {

	private TreeCtr tCtr;
	
	private TreePanel treePanel;
	
	private JTextField txtSort;
	private JTextField txtLifespan;
	private JTextField txtCostPrSquareKilometre;
	private JTextField txtRevenuePrSquareKilometre;
	
	public CreateTreeFrame() {
		tCtr = TreeCtr.getInstance();
		initilizeFrame();
		treePanel = TreePanel.getInstance();
	}
	
	private void initilizeFrame() {
		
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		
		setTitle("Create tree");
		setBounds((int) d.getWidth()/4, (int) d.getHeight() / 5, 364, 233);
		setResizable(false);
		getContentPane().setLayout(null);
		setVisible(true);
		
		
		JLabel lblSort = new JLabel("Sort");
		lblSort.setBounds(10, 15, 120, 20);
		getContentPane().add(lblSort);
		
		txtSort = new JTextField();
		txtSort.setBounds(140, 15, 200, 20);
		getContentPane().add(txtSort);
		txtSort.setColumns(10);
		
		JLabel lblLifespan = new JLabel("Lifespan");
		lblLifespan.setBounds(10, 40, 120, 20);
		getContentPane().add(lblLifespan);
		
		txtLifespan = new JTextField();
		txtLifespan.setBounds(140, 40, 200, 20);
		getContentPane().add(txtLifespan);
		txtLifespan.setColumns(10);
		
		JLabel lblCostPrSquareKilometre = new JLabel("CostPrSquareKilometre");
		lblCostPrSquareKilometre.setBounds(10, 65, 132, 20);
		getContentPane().add(lblCostPrSquareKilometre);
		
		txtCostPrSquareKilometre = new JTextField();
		txtCostPrSquareKilometre.setBounds(140, 65, 200, 20);
		getContentPane().add(txtCostPrSquareKilometre);
		txtCostPrSquareKilometre.setColumns(10);
		
		JLabel lblRevenuePrSquareKilometre = new JLabel("RevenuePrSquareKilometre");
		lblRevenuePrSquareKilometre.setBounds(10, 90, 132, 20);
		getContentPane().add(lblRevenuePrSquareKilometre);
		
		txtRevenuePrSquareKilometre = new JTextField();
		txtRevenuePrSquareKilometre.setBounds(140, 90, 200, 20);
		getContentPane().add(txtRevenuePrSquareKilometre);
		txtRevenuePrSquareKilometre.setColumns(10);
		
		
		JButton btnCreate = new JButton("Create");
		btnCreate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				createTree();
			}
		});
		btnCreate.setBounds(254, 171, 86, 20);
		getContentPane().add(btnCreate);
	}
	
	public void createTree() {
		boolean success = false;
		String sort = txtSort.getText();
		String lifespan = txtLifespan.getText();
		String costPrSquareKilometre = txtCostPrSquareKilometre.getText();
		String revenuePrSquareKilometre = txtRevenuePrSquareKilometre.getText();
		
		if(sort.isEmpty() != true && lifespan.isEmpty() != true && costPrSquareKilometre.isEmpty() != true && revenuePrSquareKilometre.isEmpty() != true) {
		success = tCtr.createTree(txtSort.getText(), Integer.parseInt(txtLifespan.getText()), Double.parseDouble(txtCostPrSquareKilometre.getText()), Double.parseDouble(txtRevenuePrSquareKilometre.getText()));
		close();
		treePanel.updatePanel(tCtr.treeListToArray());
		dispose();
		} else {
			JOptionPane.showMessageDialog(getContentPane(), "You have to fill in all textFields", "Error Message", JOptionPane.ERROR_MESSAGE);
		}
	
	}
	
	private void close() {
		setVisible(false);
		dispose();
	}
}
